package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.cache.service.IUserInfoCacheService;
import cn.wolfcode.luowowo.cache.service.IVerifyCodeCacheService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.UUID;

@Controller
public class LonginController {
    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private IVerifyCodeCacheService verifyCodeCacheService;


    @Reference
    private  IUserInfoCacheService  userInfoCacheService;
//    手机号验证 注册   手机号是否注册
    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone){
        return userInfoService.checkPhone(phone);
    }

    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone){

        AjaxResult result = new AjaxResult();
        //先检查改手机号是否验证码存在
        String verifyCode1 = verifyCodeCacheService.getVerifyCode(phone);
        if(verifyCode1!=null){
            return result.mark("验证码已存在");
        }
        //生产验证码
        String verifyCode = UUID.randomUUID().toString().substring(0, 4);
        System.err.println("你的验证码是"+verifyCode+"请在60秒使用");
        verifyCodeCacheService.setVerifyCode(phone,verifyCode);
        return null;
    }

    @RequestMapping("userRegist")
    @ResponseBody
    public  Object userRegist(String phone,String nickname,String password,String rpassword,
                              String verifyCode){
        System.out.println(verifyCode);

        return  userInfoService.userRegist(phone, nickname, password, rpassword,
                verifyCode);
    }
    @RequestMapping("userLogin")
    @ResponseBody
    public Object userLogin(String phone, String password, HttpServletResponse resp
   , HttpSession session){
        //调用登录的业务方法
        AjaxResult result = userInfoService.userLogin(phone, password);
        //如果登录成功,则返回token给客户端(使用cookie来实现
        if(result.isSuccess()){
            String token= (String) result.getData();
            Cookie cookie = new Cookie(Consts.USER_INFO_TOKEN, token);
            cookie.setPath("/");
            //返回给客户端
            resp.addCookie(cookie);
            //吧登入成功的对象存入sesion
            String userInfo = userInfoCacheService.getVerifycode(token);
            session.setAttribute(Consts.USER_INFO, JSON.parseObject(userInfo, UserInfo.class));

        }
        return result;
    }

}